package com.ywb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.ywb.entity.EduCourse;
import com.ywb.entity.EduCourseDescription;
import com.ywb.entity.EduSubject;
import com.ywb.entity.EduTeacher;
import com.ywb.entity.subject.FirstSubject;
import com.ywb.entity.vo.CourseConditionVo;
import com.ywb.entity.vo.CourseInfoVo;
import com.ywb.service.EduCourseDescriptionService;
import com.ywb.service.EduCourseService;
import com.ywb.service.EduSubjectService;
import com.ywb.service.EduTeacherService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ywb
 * @since 2021-05-27
 */
@Controller
@RequestMapping("/course")
public class EduCourseController {

    @Autowired
    EduCourseDescriptionService descriptionService;

    @Autowired
    EduCourseService courseService;

    @Autowired
    EduTeacherService teacherService;

    @Autowired
    EduSubjectService subjectService;
    /**
     * 跳转到course页面
     * @return
     */
    @GetMapping("/list")
    public String toCourseList(Model model){
        long current = 1, limit = 10;
        Page<EduCourse> page = new Page<>(current, limit);
        courseService.page(page, new QueryWrapper<EduCourse>().orderByDesc("gmt_modified"));
        model.addAttribute("page", page);
        model.addAttribute("total", page.getTotal() / page.getSize() + 1);

//        1. 获取课程名称
        List<EduCourse> list = courseService.list(null);
//       2. 获取课程名称的teacherId值
        List<String> teacherIds = list.stream().map(EduCourse::getTeacherId).collect(Collectors.toList());
//        3. 根据课程中含有的teacherId获取教师列表
        List<EduTeacher> teacherList = teacherService.list(new QueryWrapper<EduTeacher>().in("id", teacherIds));

//        4. 获取科目列表
        List<EduSubject> subjects = subjectService.list(null);

//        5.将 三个list存入request中,由list页面调用

        model.addAttribute("teacherList", teacherList);
        model.addAttribute("subjectList", subjects);

        model.addAttribute("pageName", "course/list.jsp");
        return "index";
    }

    /**
     * 跳转到course页面
     * @return
     */
    @GetMapping("/list/{current}/{limit}")
    public String toCourseList(Model model, @PathVariable long current, @PathVariable long limit){
        Page<EduCourse> page = new Page<>(current, limit);
        courseService.page(page, new QueryWrapper<EduCourse>().orderByDesc("gmt_modified"));
        model.addAttribute("page", page);
        model.addAttribute("total", page.getTotal() / page.getSize() + 1);


//        3. 根据课程中含有的teacherId获取教师列表
        List<EduTeacher> teacherList = teacherService.list(null);
//        4. 获取科目列表
        List<EduSubject> subjects = subjectService.list(null);

//        5.将 三个list存入request中,由list页面调用

        model.addAttribute("teacherList", teacherList);
        model.addAttribute("subjectList", subjects);

        model.addAttribute("pageName", "course/list.jsp");
        return "index";
    }

    /**
     * 条件查询
     * @param model
     * @return
     */
    @GetMapping("/conditionPage")
    public String conditionPage(Model model, CourseConditionVo condition){
        Page<EduCourse> page= courseService.getPageByCourseCondition(condition, 1, 999);

        model.addAttribute("page", page);
        model.addAttribute("total", page.getTotal() / page.getSize() + 1);


//        3. 根据课程中含有的teacherId获取教师列表
        List<EduTeacher> teacherList = teacherService.list(null);
//        4. 获取科目列表
        List<FirstSubject> subjects = subjectService.getFirstSubjects();

//        5.将 三个list存入request中,由list页面调用

        model.addAttribute("teacherList", teacherList);
        model.addAttribute("subjectList", subjects);

        model.addAttribute("pageName", "course/list.jsp");
        return "index";
    }


    @GetMapping("/save")
    public String toCourseSave(Model model){
        List<EduTeacher> teachers = teacherService.list(null);
        model.addAttribute("teachers",teachers);

        List<FirstSubject> firstSubjects = subjectService.getFirstSubjects();
        model.addAttribute("firstSubjects",firstSubjects);

        model.addAttribute("pageName", "course/save.jsp");

        return "index";
    }
    @PostMapping("/save")
    public String saveCourse(Model model, CourseInfoVo courseInfoVo){
        courseService.saveCourseInfo(courseInfoVo);

        model.addAttribute("pageName", "course/save.jsp");
        return "redirect:/course/list";
    }

    @GetMapping("/deleteBatchIds")
    public String deleteBatchIds(Model model, String courseIds){
        String[] split = courseIds.split(",");
        courseService.removeByIds(Arrays.asList(split));

        model.addAttribute("pageName", "course/save.jsp");

        return "redirect:/course/list";
    }

    /**
     * 根据id查询、修改课程
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    public String findById(Model model,@PathVariable String id){
        EduCourse course = courseService.getById(id);
        model.addAttribute("course", course);
        EduCourseDescription description = descriptionService.getById(id);
        model.addAttribute("description", description);
        model.addAttribute("pageName", "course/update.jsp");

        List<EduTeacher> teachers = teacherService.list(null);
        model.addAttribute("teachers",teachers);
        List<FirstSubject> firstSubjects = subjectService.getFirstSubjects();
        model.addAttribute("firstSubjects",firstSubjects);
        return "index";
    }
    @PostMapping("/updateCourse")
    public String updateCourse(EduCourse course){
        courseService.updateById(course);
        return "redirect:/course/list";
    }



}

